package com.xinwei.leetcode.每日一题;
// https://leetcode-cn.com/problems/elimination-game/
public class _390_消除游戏 {
    public int lastRemaining(int n) {
        int a1 = 1, cnt = n, step = 1, k = 0;
        while (cnt != 1){
            if (k % 2 == 0) {
                //次数为偶数
                a1 = a1 + step;
            }else {
                //次数为奇数
                a1 = (cnt % 2 == 0) ? a1 : a1 + step;
            }
            k++;
            cnt /= 2;
            step *= 2;
        }
        return a1;
    }
}
